
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 08/05/2010 20:49:22
-- Generated from EDMX file: C:\Users\Henrique Graça\Documents\Visual Studio 2010\Projects\MyGProject\DAL\ModeloDados.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Grupyz];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ArtigoDeal]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Deal] DROP CONSTRAINT [FK_ArtigoDeal];
GO
IF OBJECT_ID(N'[dbo].[FK_ComentarioDeal]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comentario] DROP CONSTRAINT [FK_ComentarioDeal];
GO
IF OBJECT_ID(N'[dbo].[FK_DealLocalidade]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Deal] DROP CONSTRAINT [FK_DealLocalidade];
GO
IF OBJECT_ID(N'[dbo].[FK_ZonaLocalidade]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Localidade] DROP CONSTRAINT [FK_ZonaLocalidade];
GO
IF OBJECT_ID(N'[dbo].[FK_ClienteComentario]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Comentario] DROP CONSTRAINT [FK_ClienteComentario];
GO
IF OBJECT_ID(N'[dbo].[FK_ParceiroArtigo]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Artigo] DROP CONSTRAINT [FK_ParceiroArtigo];
GO
IF OBJECT_ID(N'[dbo].[FK_DealOrderDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderDetail] DROP CONSTRAINT [FK_DealOrderDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderOrderDetail]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderDetail] DROP CONSTRAINT [FK_OrderOrderDetail];
GO
IF OBJECT_ID(N'[dbo].[FK_DealCart]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Cart] DROP CONSTRAINT [FK_DealCart];
GO
IF OBJECT_ID(N'[dbo].[FK_ParceiroDeal]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Deal] DROP CONSTRAINT [FK_ParceiroDeal];
GO
IF OBJECT_ID(N'[dbo].[FK_ParceiroGeoLocal]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GeoLocal] DROP CONSTRAINT [FK_ParceiroGeoLocal];
GO
IF OBJECT_ID(N'[dbo].[FK_SettingsCliente]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Settings] DROP CONSTRAINT [FK_SettingsCliente];
GO
IF OBJECT_ID(N'[dbo].[FK_ClientePremios]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Premios] DROP CONSTRAINT [FK_ClientePremios];
GO
IF OBJECT_ID(N'[dbo].[FK_TipoPremioPremios]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Premios] DROP CONSTRAINT [FK_TipoPremioPremios];
GO
IF OBJECT_ID(N'[dbo].[FK_DealDetalhe]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Detalhe] DROP CONSTRAINT [FK_DealDetalhe];
GO
IF OBJECT_ID(N'[dbo].[FK_ClientePonto]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Ponto] DROP CONSTRAINT [FK_ClientePonto];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderVoucher]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Vouchers] DROP CONSTRAINT [FK_OrderVoucher];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Artigo]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Artigo];
GO
IF OBJECT_ID(N'[dbo].[Comentario]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Comentario];
GO
IF OBJECT_ID(N'[dbo].[Deal]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Deal];
GO
IF OBJECT_ID(N'[dbo].[Localidade]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Localidade];
GO
IF OBJECT_ID(N'[dbo].[Referencia]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Referencia];
GO
IF OBJECT_ID(N'[dbo].[Zona]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Zona];
GO
IF OBJECT_ID(N'[dbo].[Parceiro]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Parceiro];
GO
IF OBJECT_ID(N'[dbo].[Cliente]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Cliente];
GO
IF OBJECT_ID(N'[dbo].[EnvioNewsletter]', 'U') IS NOT NULL
    DROP TABLE [dbo].[EnvioNewsletter];
GO
IF OBJECT_ID(N'[dbo].[Cart]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Cart];
GO
IF OBJECT_ID(N'[dbo].[Order]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Order];
GO
IF OBJECT_ID(N'[dbo].[OrderDetail]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderDetail];
GO
IF OBJECT_ID(N'[dbo].[GeoLocal]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GeoLocal];
GO
IF OBJECT_ID(N'[dbo].[Settings]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Settings];
GO
IF OBJECT_ID(N'[dbo].[Premios]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Premios];
GO
IF OBJECT_ID(N'[dbo].[TipoPremio]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TipoPremio];
GO
IF OBJECT_ID(N'[dbo].[Detalhe]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Detalhe];
GO
IF OBJECT_ID(N'[dbo].[Ponto]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Ponto];
GO
IF OBJECT_ID(N'[dbo].[Vouchers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Vouchers];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Artigo'
CREATE TABLE [dbo].[Artigo] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nome] nvarchar(max)  NOT NULL,
    [Disponivel] bit  NOT NULL,
    [Imagem] nvarchar(max)  NOT NULL,
    [ParceiroId] int  NOT NULL
);
GO

-- Creating table 'Comentario'
CREATE TABLE [dbo].[Comentario] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [DealId] int  NULL,
    [Texto] nvarchar(max)  NOT NULL,
    [NotaPositiva] int  NULL,
    [NotaNegativa] int  NULL,
    [IsAdmin] bit  NULL,
    [ClienteId] int  NOT NULL,
    [Data] datetime  NOT NULL
);
GO

-- Creating table 'Deal'
CREATE TABLE [dbo].[Deal] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ArtigoId] int  NOT NULL,
    [QuantidadeMinima] int  NOT NULL,
    [QuantidadeMaxima] int  NOT NULL,
    [PrecoVenda] decimal(18,2)  NOT NULL,
    [PrecoDesconto] decimal(18,2)  NOT NULL,
    [Descricao] nvarchar(max)  NOT NULL,
    [Texto] nvarchar(max)  NOT NULL,
    [Data] datetime  NOT NULL,
    [LocalidadeId] int  NOT NULL,
    [TotalComprado] int  NOT NULL,
    [Expira] datetime  NOT NULL,
    [Disponivel] bit  NOT NULL,
    [ParceiroId] int  NOT NULL,
    [Favorito] bit  NOT NULL,
    [DayDeal] bit  NOT NULL,
    [SideDeal] bit  NOT NULL
);
GO

-- Creating table 'Localidade'
CREATE TABLE [dbo].[Localidade] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nome] nvarchar(150)  NOT NULL,
    [ZonaId] int  NOT NULL
);
GO

-- Creating table 'Referencia'
CREATE TABLE [dbo].[Referencia] (
    [IdClienteReferencia] int  NOT NULL,
    [IdClienteReferenciado] int  NOT NULL,
    [Activo] bit  NOT NULL,
    [Data] datetime  NOT NULL,
    [DataLimite] datetime  NOT NULL,
    [Valor] decimal(18,0)  NOT NULL
);
GO

-- Creating table 'Zona'
CREATE TABLE [dbo].[Zona] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nome] nvarchar(100)  NOT NULL
);
GO

-- Creating table 'Parceiro'
CREATE TABLE [dbo].[Parceiro] (
    [Nome] nvarchar(max)  NOT NULL,
    [NIF] int  NULL,
    [Email] nvarchar(max)  NULL,
    [Telefone] nvarchar(max)  NULL,
    [Morada] nvarchar(max)  NULL,
    [CodPostal] nvarchar(max)  NULL,
    [Localidade] nvarchar(max)  NULL,
    [Id] int IDENTITY(1,1) NOT NULL,
    [Url] nvarchar(max)  NULL,
    [Apresentacao] nvarchar(max)  NOT NULL,
    [Contactar] bit  NOT NULL,
    [NomeNegocio] nvarchar(max)  NULL
);
GO

-- Creating table 'Cliente'
CREATE TABLE [dbo].[Cliente] (
    [Nome] nvarchar(max)  NOT NULL,
    [DataNascimento] datetime  NULL,
    [Sexo] nvarchar(20)  NULL,
    [Referencia] nvarchar(max)  NOT NULL,
    [IDSocial] nvarchar(max)  NOT NULL,
    [DisplayName] nvarchar(50)  NULL,
    [UriImagem] nvarchar(max)  NULL,
    [Email] nvarchar(100)  NULL,
    [Telefone] nvarchar(20)  NULL,
    [Morada] nvarchar(max)  NULL,
    [CodPostal] nvarchar(20)  NULL,
    [Localidade] nvarchar(100)  NULL,
    [Id] int IDENTITY(1,1) NOT NULL,
    [Password] nvarchar(max)  NULL,
    [TornarSocial] bit  NOT NULL
);
GO

-- Creating table 'EnvioNewsletter'
CREATE TABLE [dbo].[EnvioNewsletter] (
    [Email] nvarchar(100)  NOT NULL,
    [DataEnvio] datetime  NULL,
    [Id] int IDENTITY(1,1) NOT NULL
);
GO

-- Creating table 'Cart'
CREATE TABLE [dbo].[Cart] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Quantidade] int  NOT NULL,
    [IdSession] nvarchar(max)  NOT NULL,
    [DealId] int  NOT NULL,
    [ClienteId] int  NULL,
    [Oferta] bit  NOT NULL,
    [DadosOferta_NomeDe] nvarchar(max)  NULL,
    [DadosOferta_NomePara] nvarchar(max)  NULL,
    [DadosOferta_EmailDest] nvarchar(max)  NULL,
    [DadosOferta_Mensagem] nvarchar(max)  NULL,
    [DadosOferta_EuImprimo] bit  NOT NULL,
    [Pontos] int  NOT NULL,
    [Desconto] int  NOT NULL
);
GO

-- Creating table 'Order'
CREATE TABLE [dbo].[Order] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [PrimeiroNome] nvarchar(100)  NOT NULL,
    [UltimoNome] nvarchar(100)  NOT NULL,
    [Morada] nvarchar(max)  NULL,
    [CodPostal] nvarchar(50)  NULL,
    [Localidade] nvarchar(100)  NULL,
    [Telefone] nvarchar(20)  NULL,
    [Email] nvarchar(150)  NOT NULL,
    [Total] decimal(18,2)  NULL,
    [Data] datetime  NOT NULL,
    [IdCliente] int  NOT NULL,
    [IdPaypal] nvarchar(max)  NULL,
    [Pendente] bit  NULL,
    [ValorDesconto] int  NULL,
    [CodReferencia] nvarchar(max)  NULL,
    [Concluido] bit  NOT NULL
);
GO

-- Creating table 'OrderDetail'
CREATE TABLE [dbo].[OrderDetail] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [DealId] int  NOT NULL,
    [OrderId] int  NOT NULL,
    [Quantidade] int  NOT NULL,
    [ValorUn] decimal(18,2)  NOT NULL,
    [Oferta] bit  NOT NULL,
    [DadosOferta_NomeDe] nvarchar(max)  NULL,
    [DadosOferta_NomePara] nvarchar(max)  NULL,
    [DadosOferta_EmailDest] nvarchar(max)  NULL,
    [DadosOferta_Mensagem] nvarchar(max)  NULL,
    [DadosOferta_EuImprimo] bit  NOT NULL,
    [Pontos] int  NOT NULL,
    [Desconto] int  NOT NULL
);
GO

-- Creating table 'GeoLocal'
CREATE TABLE [dbo].[GeoLocal] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ParceiroId] int  NOT NULL,
    [Latitude] nvarchar(100)  NULL,
    [Longitude] nvarchar(100)  NULL
);
GO

-- Creating table 'Settings'
CREATE TABLE [dbo].[Settings] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ClienteId] int  NOT NULL,
    [DealExpire] bit  NOT NULL,
    [FriendReferenced] bit  NOT NULL,
    [PremioAdicionado] bit  NOT NULL
);
GO

-- Creating table 'Premios'
CREATE TABLE [dbo].[Premios] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ClienteId] int  NOT NULL,
    [Descricao] nvarchar(max)  NOT NULL,
    [Valor] decimal(18,0)  NOT NULL,
    [Data] datetime  NOT NULL,
    [Activo] bit  NOT NULL,
    [TipoPremioId] int  NOT NULL
);
GO

-- Creating table 'TipoPremio'
CREATE TABLE [dbo].[TipoPremio] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Nome] nvarchar(150)  NOT NULL,
    [Activo] bit  NOT NULL
);
GO

-- Creating table 'Detalhe'
CREATE TABLE [dbo].[Detalhe] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [DealId] int  NOT NULL,
    [Texto] nvarchar(max)  NOT NULL,
    [Tipo] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Ponto'
CREATE TABLE [dbo].[Ponto] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Pontos] int  NOT NULL,
    [ClienteId] int  NOT NULL
);
GO

-- Creating table 'Vouchers'
CREATE TABLE [dbo].[Vouchers] (
    [Validade] datetime  NOT NULL,
    [CodSeguranca] nvarchar(max)  NOT NULL,
    [Referencia] nvarchar(max)  NOT NULL,
    [OrderId] int  NOT NULL,
    [Filename] nvarchar(max)  NOT NULL,
    [DataCriacao] datetime  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Artigo'
ALTER TABLE [dbo].[Artigo]
ADD CONSTRAINT [PK_Artigo]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Comentario'
ALTER TABLE [dbo].[Comentario]
ADD CONSTRAINT [PK_Comentario]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Deal'
ALTER TABLE [dbo].[Deal]
ADD CONSTRAINT [PK_Deal]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Localidade'
ALTER TABLE [dbo].[Localidade]
ADD CONSTRAINT [PK_Localidade]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [IdClienteReferencia], [IdClienteReferenciado] in table 'Referencia'
ALTER TABLE [dbo].[Referencia]
ADD CONSTRAINT [PK_Referencia]
    PRIMARY KEY CLUSTERED ([IdClienteReferencia], [IdClienteReferenciado] ASC);
GO

-- Creating primary key on [Id] in table 'Zona'
ALTER TABLE [dbo].[Zona]
ADD CONSTRAINT [PK_Zona]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Parceiro'
ALTER TABLE [dbo].[Parceiro]
ADD CONSTRAINT [PK_Parceiro]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Cliente'
ALTER TABLE [dbo].[Cliente]
ADD CONSTRAINT [PK_Cliente]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'EnvioNewsletter'
ALTER TABLE [dbo].[EnvioNewsletter]
ADD CONSTRAINT [PK_EnvioNewsletter]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Cart'
ALTER TABLE [dbo].[Cart]
ADD CONSTRAINT [PK_Cart]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Order'
ALTER TABLE [dbo].[Order]
ADD CONSTRAINT [PK_Order]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OrderDetail'
ALTER TABLE [dbo].[OrderDetail]
ADD CONSTRAINT [PK_OrderDetail]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'GeoLocal'
ALTER TABLE [dbo].[GeoLocal]
ADD CONSTRAINT [PK_GeoLocal]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Settings'
ALTER TABLE [dbo].[Settings]
ADD CONSTRAINT [PK_Settings]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Premios'
ALTER TABLE [dbo].[Premios]
ADD CONSTRAINT [PK_Premios]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'TipoPremio'
ALTER TABLE [dbo].[TipoPremio]
ADD CONSTRAINT [PK_TipoPremio]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Detalhe'
ALTER TABLE [dbo].[Detalhe]
ADD CONSTRAINT [PK_Detalhe]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Ponto'
ALTER TABLE [dbo].[Ponto]
ADD CONSTRAINT [PK_Ponto]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [OrderId] in table 'Vouchers'
ALTER TABLE [dbo].[Vouchers]
ADD CONSTRAINT [PK_Vouchers]
    PRIMARY KEY CLUSTERED ([OrderId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ArtigoId] in table 'Deal'
ALTER TABLE [dbo].[Deal]
ADD CONSTRAINT [FK_ArtigoDeal]
    FOREIGN KEY ([ArtigoId])
    REFERENCES [dbo].[Artigo]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ArtigoDeal'
CREATE INDEX [IX_FK_ArtigoDeal]
ON [dbo].[Deal]
    ([ArtigoId]);
GO

-- Creating foreign key on [DealId] in table 'Comentario'
ALTER TABLE [dbo].[Comentario]
ADD CONSTRAINT [FK_ComentarioDeal]
    FOREIGN KEY ([DealId])
    REFERENCES [dbo].[Deal]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ComentarioDeal'
CREATE INDEX [IX_FK_ComentarioDeal]
ON [dbo].[Comentario]
    ([DealId]);
GO

-- Creating foreign key on [LocalidadeId] in table 'Deal'
ALTER TABLE [dbo].[Deal]
ADD CONSTRAINT [FK_DealLocalidade]
    FOREIGN KEY ([LocalidadeId])
    REFERENCES [dbo].[Localidade]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DealLocalidade'
CREATE INDEX [IX_FK_DealLocalidade]
ON [dbo].[Deal]
    ([LocalidadeId]);
GO

-- Creating foreign key on [ZonaId] in table 'Localidade'
ALTER TABLE [dbo].[Localidade]
ADD CONSTRAINT [FK_ZonaLocalidade]
    FOREIGN KEY ([ZonaId])
    REFERENCES [dbo].[Zona]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ZonaLocalidade'
CREATE INDEX [IX_FK_ZonaLocalidade]
ON [dbo].[Localidade]
    ([ZonaId]);
GO

-- Creating foreign key on [ClienteId] in table 'Comentario'
ALTER TABLE [dbo].[Comentario]
ADD CONSTRAINT [FK_ClienteComentario]
    FOREIGN KEY ([ClienteId])
    REFERENCES [dbo].[Cliente]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ClienteComentario'
CREATE INDEX [IX_FK_ClienteComentario]
ON [dbo].[Comentario]
    ([ClienteId]);
GO

-- Creating foreign key on [ParceiroId] in table 'Artigo'
ALTER TABLE [dbo].[Artigo]
ADD CONSTRAINT [FK_ParceiroArtigo]
    FOREIGN KEY ([ParceiroId])
    REFERENCES [dbo].[Parceiro]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ParceiroArtigo'
CREATE INDEX [IX_FK_ParceiroArtigo]
ON [dbo].[Artigo]
    ([ParceiroId]);
GO

-- Creating foreign key on [DealId] in table 'OrderDetail'
ALTER TABLE [dbo].[OrderDetail]
ADD CONSTRAINT [FK_DealOrderDetail]
    FOREIGN KEY ([DealId])
    REFERENCES [dbo].[Deal]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DealOrderDetail'
CREATE INDEX [IX_FK_DealOrderDetail]
ON [dbo].[OrderDetail]
    ([DealId]);
GO

-- Creating foreign key on [OrderId] in table 'OrderDetail'
ALTER TABLE [dbo].[OrderDetail]
ADD CONSTRAINT [FK_OrderOrderDetail]
    FOREIGN KEY ([OrderId])
    REFERENCES [dbo].[Order]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderOrderDetail'
CREATE INDEX [IX_FK_OrderOrderDetail]
ON [dbo].[OrderDetail]
    ([OrderId]);
GO

-- Creating foreign key on [DealId] in table 'Cart'
ALTER TABLE [dbo].[Cart]
ADD CONSTRAINT [FK_DealCart]
    FOREIGN KEY ([DealId])
    REFERENCES [dbo].[Deal]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DealCart'
CREATE INDEX [IX_FK_DealCart]
ON [dbo].[Cart]
    ([DealId]);
GO

-- Creating foreign key on [ParceiroId] in table 'Deal'
ALTER TABLE [dbo].[Deal]
ADD CONSTRAINT [FK_ParceiroDeal]
    FOREIGN KEY ([ParceiroId])
    REFERENCES [dbo].[Parceiro]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ParceiroDeal'
CREATE INDEX [IX_FK_ParceiroDeal]
ON [dbo].[Deal]
    ([ParceiroId]);
GO

-- Creating foreign key on [ParceiroId] in table 'GeoLocal'
ALTER TABLE [dbo].[GeoLocal]
ADD CONSTRAINT [FK_ParceiroGeoLocal]
    FOREIGN KEY ([ParceiroId])
    REFERENCES [dbo].[Parceiro]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ParceiroGeoLocal'
CREATE INDEX [IX_FK_ParceiroGeoLocal]
ON [dbo].[GeoLocal]
    ([ParceiroId]);
GO

-- Creating foreign key on [ClienteId] in table 'Settings'
ALTER TABLE [dbo].[Settings]
ADD CONSTRAINT [FK_SettingsCliente]
    FOREIGN KEY ([ClienteId])
    REFERENCES [dbo].[Cliente]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SettingsCliente'
CREATE INDEX [IX_FK_SettingsCliente]
ON [dbo].[Settings]
    ([ClienteId]);
GO

-- Creating foreign key on [ClienteId] in table 'Premios'
ALTER TABLE [dbo].[Premios]
ADD CONSTRAINT [FK_ClientePremios]
    FOREIGN KEY ([ClienteId])
    REFERENCES [dbo].[Cliente]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ClientePremios'
CREATE INDEX [IX_FK_ClientePremios]
ON [dbo].[Premios]
    ([ClienteId]);
GO

-- Creating foreign key on [TipoPremioId] in table 'Premios'
ALTER TABLE [dbo].[Premios]
ADD CONSTRAINT [FK_TipoPremioPremios]
    FOREIGN KEY ([TipoPremioId])
    REFERENCES [dbo].[TipoPremio]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TipoPremioPremios'
CREATE INDEX [IX_FK_TipoPremioPremios]
ON [dbo].[Premios]
    ([TipoPremioId]);
GO

-- Creating foreign key on [DealId] in table 'Detalhe'
ALTER TABLE [dbo].[Detalhe]
ADD CONSTRAINT [FK_DealDetalhe]
    FOREIGN KEY ([DealId])
    REFERENCES [dbo].[Deal]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DealDetalhe'
CREATE INDEX [IX_FK_DealDetalhe]
ON [dbo].[Detalhe]
    ([DealId]);
GO

-- Creating foreign key on [ClienteId] in table 'Ponto'
ALTER TABLE [dbo].[Ponto]
ADD CONSTRAINT [FK_ClientePonto]
    FOREIGN KEY ([ClienteId])
    REFERENCES [dbo].[Cliente]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ClientePonto'
CREATE INDEX [IX_FK_ClientePonto]
ON [dbo].[Ponto]
    ([ClienteId]);
GO

-- Creating foreign key on [OrderId] in table 'Vouchers'
ALTER TABLE [dbo].[Vouchers]
ADD CONSTRAINT [FK_OrderVoucher]
    FOREIGN KEY ([OrderId])
    REFERENCES [dbo].[Order]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------